<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class News extends Base
{
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($banner_type)&&$banner_type!="")
            {
                $map['n_type'] = $banner_type;
            }
            if(isset($n_top)&&$n_top!="")
            {
                $map['n_top'] = $n_top;
            }
            if(isset($banner_status)&&$banner_status!="")
            {
                $map['n_status'] = $banner_status;
            }
            if(isset($n_title)&&$n_title!="")
            {
                $map['n_title|n_desc'] = ['like',"%" . $n_title . "%"];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_news')->where($map)->count();// 获取总条数
            $lists = Db::table('up_news')
                ->where($map)
                ->field('n_content',true)
                ->page($Nowpage,$limits)
                ->order('n_top desc,n_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $n_title = trim(input('n_title'), ' ');
            $n_image = input('n_image');
            $n_desc = trim(input('n_desc'), ' ');
            $n_top = input('n_top');
            $n_type = input('n_type');
            $n_status = input('n_status');
            $n_see = (int)input('n_see');
            $n_content = trim(input('n_content'), ' ');
            if(!$n_title || !$n_image){
                return json(['code' => 100, 'msg' => '标题和图片不能为空']);
            }

            $data = [
                'n_title' => $n_title,
                'n_image' => $n_image,
                'n_desc' => $n_desc,
                'n_top' => $n_top,
                'n_type' => $n_type,
                'n_status' => $n_status,
                'n_see' => $n_see,
                'n_content' => $n_content,
                'n_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_news')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        // 商品
        return $this->fetch('news/banneradd');
    }
    
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            $n_id = input('n_id');
            $n_title = trim(input('n_title'), ' ');
            $n_image = input('n_image');
            $n_desc = trim(input('n_desc'), ' ');
            $n_top = input('n_top');
            $n_type = input('n_type');
            $n_status = input('n_status');
            $n_see = (int)input('n_see');
            $n_content = trim(input('n_content'), ' ');
            if(!$n_title || !$n_image){
                return json(['code' => 100, 'msg' => '标题和图片不能为空']);
            }

            $data = [
                'n_title' => $n_title,
                'n_image' => $n_image,
                'n_desc' => $n_desc,
                'n_top' => $n_top,
                'n_type' => $n_type,
                'n_status' => $n_status,
                'n_see' => $n_see,
                'n_content' => $n_content,
                'n_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_news')->where('n_id', $n_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $n_id = input('n_id');
        // banner
        $banner = Db::table('up_news')
            ->where('n_id', $n_id)
            ->find();
        
        $this->assign([
            'n_id' => $n_id,
            'banner' => $banner
        ]);
        return $this->fetch('news/banneredit');
    }
    
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'banner_sort'=>input('value'),
        ];
        $res = Db::name('banner')->where('banner_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $banner_id = input('id');
        $res = Db::table('up_news')->where('n_id', $banner_id)->setField('n_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 100, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $banner_id = input('id');
        $res = Db::table('up_news')->where('n_id', $banner_id)->setField('n_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 100, 'msg' => '下架失败']);
        }
    }
    /**
     * 置顶
     * @return \think\response\Json
     */
    public function bannerTop(){
        $banner_id = input('id');
        $res = Db::table('up_news')->where('n_id', $banner_id)->setField('n_top', 2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 取消置顶
     * @return \think\response\Json
     */
    public function bannerTopno(){
        $banner_id = input('id');
        $res = Db::table('up_news')->where('n_id', $banner_id)->setField('n_top', 1);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDel(){
        $banner_id = input('id');
        $res = Db::table('up_news')->where('n_id', $banner_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
}